package dLogistic.application.service;

import dLogistic.domain.model.Paczka.TrasaPaczki;
import dLogistic.infrastructure.persistence.hibernate.HibernateUtil;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import java.util.Date;
import java.util.List;

/**
 * Created by Przemek on 2014-10-26.
 */
public class PaczkaService {
    private static SessionFactory factory;

    public boolean przekazaniePaczki(String uuid_paczka, String przekazujacy, String odbierajacy, Integer przekazanie){
        factory = HibernateUtil.getSessionFactory();
        Session session = factory.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        TrasaPaczki trasaPaczki = new TrasaPaczki().znajdzTrase(uuid_paczka, przekazujacy, odbierajacy);
        if (trasaPaczki == null){
            transaction.rollback();
            return false;
        }
        Date dt = new Date();
        if (przekazanie == 0){
            trasaPaczki.ustawDataStart(dt.toString());
        }
        else {
            trasaPaczki.ustawDataEnd(dt.toString());
        }
        session.update(trasaPaczki);
        transaction.commit();
        return true;
    }

    public String StatusPaczki(String uuid_paczka){
        factory = HibernateUtil.getSessionFactory();
        Session session = factory.getCurrentSession();
        Transaction transaction = session.beginTransaction();

        String sql1 = "select * from Paczka where uuid = :uuid";
        SQLQuery query1 = session.createSQLQuery(sql1);
        query1.setParameter("uuid", uuid_paczka);
        List results1 = query1.list();
        if (results1.isEmpty()){
            transaction.rollback();
            return "Nie istnieje paczka o takim uuid";

        }


        String sql = "select * from TrasaPaczki where uuid_paczka = :uuid_paczka and (DataEnd = \"\" or DataStart = \"\") order by typ, kolejnosc limit 1";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(TrasaPaczki.class);
        query.setParameter("uuid_paczka", uuid_paczka);

        List results = query.list();
        transaction.rollback();
        if (results.isEmpty()){

            return "Paczka dostarczona do klienta";
        }

        TrasaPaczki trasaPaczki = (TrasaPaczki) results.get(0);
        if(trasaPaczki.typ() == 0){
            if (trasaPaczki.DataStart().toString().equals("")){
                return "Paczka oczekuje na odbiór od nadawcy przez kuriera";
            }
            else {
                return "Paczka w drodze od nadawcy na magazyn: " + trasaPaczki.RoadEnd();
            }
        }
        else if(trasaPaczki.typ() == 2){
            if (trasaPaczki.DataStart().toString().equals("")){
                return "Paczka w magazynie: " + trasaPaczki.RoadStart() + " oczekuje na dostarczenie do adresata: " + trasaPaczki.RoadEnd();
            }
            else {
                return "Paczka w drodze do adesata: " + trasaPaczki.RoadEnd();
            }
        }
        else {
            if (trasaPaczki.DataStart().toString().equals("")){
                return "Paczka w magazynie: " + trasaPaczki.RoadStart();
            }
            else {
                return "Paczka w drodze z magazynu: " + trasaPaczki.RoadStart() +  " do magazynu: " + trasaPaczki.RoadEnd();
            }
        }

    }

    public Integer StatusPaczkiInt(String uuid_paczka){
        factory = HibernateUtil.getSessionFactory();
        Session session = factory.getCurrentSession();
        Transaction transaction = session.beginTransaction();

        String sql1 = "select * from Paczka where uuid = :uuid";
        SQLQuery query1 = session.createSQLQuery(sql1);
        query1.setParameter("uuid", uuid_paczka);
        List results1 = query1.list();
        if (results1.isEmpty()){
            transaction.rollback();
            return -1;

        }


        String sql = "select * from TrasaPaczki where uuid_paczka = :uuid_paczka and (DataEnd = \"\" or DataStart = \"\") order by typ, kolejnosc limit 1";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(TrasaPaczki.class);
        query.setParameter("uuid_paczka", uuid_paczka);

        List results = query.list();
        transaction.rollback();
        if (results.isEmpty()){

            return 6;
        }

        TrasaPaczki trasaPaczki = (TrasaPaczki) results.get(0);
        if(trasaPaczki.typ() == 0){
            if (trasaPaczki.DataStart().toString().equals("")){
                return 0;
            }
            else {
                return 1;
            }
        }
        else if(trasaPaczki.typ() == 2){
            if (trasaPaczki.DataStart().toString().equals("")){
                return 4;
            }
            else {
                return 5;
            }
        }
        else {
            if (trasaPaczki.DataStart().toString().equals("")){
                return 2;
            }
            else {
                return 3;
            }
        }

    }
   
}
